<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class MY_Model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    /**
     * 
     * @param type $table
     * @param type $where
     * @param type $orderBy
     * @param type $page
     * @param type $limit
     * @param type $field
     * @return type
     */
    function getListByCondition($table, $where = array(), $orderBy = array(), $start = 0, $limit = -1, $field = "*") {

        $this->db->select($field);
        $this->db->from($table);
        foreach ($where as $key => $value) {
            if (is_array($value)) {
                foreach ($value as $key1 => $value1) {
                    $this->db->where($key1, $value1);
                }
            } else {
                $this->db->where($key, $value);
            }
        }
        foreach ($orderBy as $keyo => $valueo) {
            if (is_array($valueo)) {
                foreach ($valueo as $keyo1 => $valueo1) {
                    $this->db->order_by($keyo1, $valueo1);
                }
            } else {
                $this->db->order_by($keyo, $valueo);
            }
        }
        if ($limit > 0) {
            $this->db->limit($limit, $start);
        }
        $query = $this->db->get();
//        echo $this->db->last_query();
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
    }

    /**
     * 获取条数 
     * @param type $table
     * @param type $where
     * @param type $orderBy

     * @return type
     */
    function getCountByCondtion($table, $where = array(), $orderBy = array()) {
        $this->db->select("count (*) as cc");
        $this->db->from($table);
        foreach ($where as $key => $value) {
            if (is_array($value)) {
                foreach ($value as $key1 => $value1) {
                    $this->db->where($key1, $value1);
                }
            } else {
                $this->db->where($key, $value);
            }
        }
        foreach ($orderBy as $keyo => $valueo) {
            if (is_array($valueo)) {
                foreach ($valueo as $keyo1 => $valueo1) {
                    $this->db->order_by($keyo1, $valueo1);
                }
            } else {
                $this->db->order_by($keyo, $valueo);
            }
        }

        $query = $this->db->get();
        echo $this->db->last_query();
        if ($query->num_rows() > 0) {
            $res = $query->result_array();
            return $res[0]['cc'];
        }
        return 0;
    }

}

class SB_Model extends MY_Model {

    function __construct() {
        parent::__construct();
    }

}
